/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*!
 * @file lin_drv.c
 * @brief This file defines lin driver functions
 */

/*******Includes***************************************************************/
#include "uart_lin_drv.h"

/*******Definitions************************************************************/

/*******Variables**************************************************************/

/*******Prototypes*************************************************************/

/*******Private Functions******************************************************/

/*******Code*******************************************************************/

/*******************************************************************************
 *** Function Name : LIN_Init
 *** Description   : Initializes an instance LIN Hardware Interface for LIN Network
 ******************************************************************************/
errcode_t LIN_Init(uint32_t instance,
                   lin_state_t* linState,
                   lin_config_t* linConfig)
{
    errcode_t errCode;

    errCode = UART_LIN_Init(instance, linState, linConfig);

    return errCode;
}

/*******************************************************************************
 *** Function Name : LIN_Deinit
 *** Description   : Shuts down the LIN Hardware Interface by disabling interrupts
 ***                 and transmitter/receiver
 ******************************************************************************/
void LIN_Deinit(uint32_t instance)
{
    UART_LIN_Deinit(instance);
}


/*******************************************************************************
 *** Function Name : UART_LIN_GetDefaultConfig
 *** Description   : Initializes the LIN user configuration structure with default values
 ******************************************************************************/
void LIN_GetDefaultConfig(bool isMaster,
                          lin_config_t* linConfig)
{
    UART_LIN_GetDefaultConfig(isMaster, linConfig);
}

/*******************************************************************************
 *** Function Name : LIN_InstallCallback
 *** Description   : Installs callback function that is used for LIN_IRQHandlerProcess
 ******************************************************************************/
void LIN_InstallCallback(uint32_t instance,
                         lin_callback_t callback,
                         void* param)
{
    UART_LIN_InstallCallback(instance, callback, param);
}

/*******************************************************************************
 *** Function Name : LIN_SendFrameDataBlocking
 *** Description   : Sends Frame data out through the LIN Hardware using blocking method
 ******************************************************************************/
errcode_t LIN_SendFrameDataBlocking(uint32_t instance,
                                    const uint8_t* txBuff,
                                    uint8_t txSize,
                                    uint32_t timeoutmSec)
{
    errcode_t errCode;

    errCode = UART_LIN_SendFrameDataBlocking(instance, txBuff, txSize, timeoutmSec);

    return errCode;
}

/*******************************************************************************
 *** Function Name : LIN_SendFrameData
 *** Description   : Sends frame data out through the LIN Hardware using non-blocking method
 ******************************************************************************/
errcode_t LIN_SendFrameData(uint32_t instance,
                            const uint8_t* txBuff,
                            uint8_t txSize)
{
    errcode_t errCode;

    errCode = UART_LIN_SendFrameData(instance, txBuff, txSize);

    return errCode;
}

/*******************************************************************************
 *** Function Name : LIN_GetTransmitStatus
 *** Description   : Get status of an on-going non-blocking transmission
 ******************************************************************************/
errcode_t LIN_GetTransmitStatus(uint32_t instance,
                                uint8_t* bytesRemaining)
{
    errcode_t errCode;

    errCode = UART_LIN_GetTransmitStatus(instance, bytesRemaining);

    return errCode;
}

/*******************************************************************************
 *** Function Name : LIN_ReceiveFrameDataBlocking
 *** Description   : Receives frame data through the LIN Hardware using blocking method
 ******************************************************************************/
errcode_t LIN_ReceiveFrameDataBlocking(uint32_t instance,
                                       uint8_t* rxBuff,
                                       uint8_t rxSize,
                                       uint32_t timeoutmSec)
{
    errcode_t errCode;

    errCode = UART_LIN_RecvFrmDataBlocking(instance, rxBuff, rxSize, timeoutmSec);

    return errCode;
}

/*******************************************************************************
 *** Function Name : LIN_ReceiveFrameDataBlocking
 *** Description   : Receives frame data through the LIN Hardware using non-blocking method
 ******************************************************************************/
errcode_t LIN_ReceiveFrameData(uint32_t instance,
                               uint8_t* rxBuff,
                               uint8_t rxSize)
{
    errcode_t errCode;

    errCode = UART_LIN_RecvFrmData(instance, rxBuff, rxSize);

    return errCode;
}

/*******************************************************************************
 *** Function Name : LIN_GetReceiveStatus
 *** Description   : Get status of an on-going non-blocking reception
 ******************************************************************************/
errcode_t LIN_GetReceiveStatus(uint32_t instance,
                               uint8_t* bytesRemaining)
{
    errcode_t errCode;

    errCode = UART_LIN_GetReceiveStatus(instance, bytesRemaining);

    return errCode;
}

/*******************************************************************************
 *** Function Name : LIN_AbortTransferData
 *** Description   : Aborts an on-going non-blocking transmission/reception
 ******************************************************************************/
void LIN_AbortTransferData(uint32_t instance)
{
    UART_LIN_AbortTransferData(instance);
}

/*******************************************************************************
 *** Function Name : LIN_GoToSleepState
 *** Description   : Puts current LIN node to sleep mode
 ******************************************************************************/
void LIN_GotoSleepState(uint32_t instance)
{
    UART_LIN_GoToSleepState(instance);
}

/*******************************************************************************
 *** Function Name : LIN_GotoIdleState
 *** Description   : Puts current LIN node to Idle state
 ******************************************************************************/
void LIN_GotoIdleState(uint32_t instance)
{
    UART_LIN_GotoIdleState(instance);
}

/*******************************************************************************
 *** Function Name : LIN_GotoIdleState
 *** Description   : Sends a wakeup signal through the LIN Hardware
 ******************************************************************************/
errcode_t LIN_SendWakeupSignal(uint32_t instance)
{
    errcode_t errCode;

    errCode = UART_LIN_SendWakeupSignal(instance);

    return errCode;
}

/*******************************************************************************
 *** Function Name : LIN_GetNodeState
 *** Description   : Get the current LIN node state
 ******************************************************************************/
lin_node_state_t LIN_GetNodeState(uint32_t instance)
{
    lin_node_state_t retState;

    retState = UART_LIN_GetNodeState(instance);

    return retState;
}

/*******************************************************************************
 *** Function Name : LIN_TimeoutService
 *** Description   : Callback function for Timer Interrupt Handler
 ******************************************************************************/
void LIN_TimeoutService(uint32_t instance)
{
    UART_LIN_TimeoutService(instance);
}

/*******************************************************************************
 *** Function Name : LIN_TimeoutService
 *** Description   : Set Value for Timeout Counter
 ******************************************************************************/
void LIN_SetTimeoutCounter(uint32_t instance,
                           uint32_t timeoutVal)
{
    UART_LIN_SetTimeoutCounter(instance, timeoutVal);
}

/*******************************************************************************
 *** Function Name : LIN_MasterSendHeader
 *** Description   : Sends frame header out through the LIN Hardware
 ******************************************************************************/
errcode_t LIN_MasterSendHeader(uint32_t instance,
                               uint8_t ID)
{
    errcode_t errCode;

    errCode = UART_LIN_MasterSendHeader(instance, ID);

    return errCode;
}

/*******************************************************************************
 *** Function Name : LIN_SetIntEnable
 *** Description   : Set LIN hardware interrupts to enable or disable
 ******************************************************************************/
void LIN_SetIntEnable(uint32_t instance, bool enable)
{
    LIN_LIN_SetIntEnable(instance, enable);
}

/*******************************************************************************
 *** Function Name : LIN_GetBusPhyState
 *** Description   : Gets the bus external physical state
 ******************************************************************************/
lin_bus_state_t LIN_GetBusPhyState(uint32_t instance)
{
    return (UART_LIN_GetBusPhyState(instance));
}

/*******EOF********************************************************************/
